//StatefulWidget又被称为有状态组件，开发者可以根据用户的操作来选择性的更新界面上的组件。

import 'dart:math';

import 'package:flutter/material.dart';

class MyStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new MyStatefulWidgetState();
  }
}

//MyStatefulWidgetState继承于State,同样它需要返回真正页面上展示的内容，
// 但是这个组件内你可以调用 setState(() {})的回调函数来实现界面效果的更新。
class MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('有状态组件'),
      ),
      body: Center(
        child: Text(
          '$_count',
          style: Theme.of(context).textTheme.display1,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        child: Icon(Icons.add),
      ),
    );
  }

  void _incrementCounter() {
    setState(() {
      _count++;
    });
  }
}

//练习

class MyIndexedStack10 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return new MyIndexedStackState();
  }
}

class MyIndexedStackState extends State<MyIndexedStack10> {
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: AppBar(
        title: Text('状态练习'),
      ),
      body: IndexedStack(
        index: _index,
        children: <Widget>[
          Icon(
            Icons.add,
            color: Colors.red,
            size: 100.0,
          ),
          Icon(
            Icons.message,
            color: Colors.orange,
            size: 100.0,
          ),
          Icon(
            Icons.person,
            color: Colors.pink,
            size: 100.0,
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getThree,
        child: Icon(Icons.work),
      ),
    );
  }

  void _getThree() {
    setState(() {
      _index = Random().nextInt(3);
    });
  }
}
